﻿using CK.Project.Web.Core;
using CK.Project.Web.EntityFramework.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CK.Project.Web.Application
{
    /// <summary>
    /// 用户模块接口实现类
    /// </summary>
    public class UserAppService : IUserAppService
    {
        private readonly PgRepository<User> userRepository;

        public UserAppService(PgRepository<User> userRepository)
        {
            this.userRepository = userRepository;
        }


        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="userListDto"></param>
        /// <returns></returns>
        public OUserListDto UserList(UserListDto userListDto)
        {
            //实例方法输出对象
            OUserListDto oUserListDto = new OUserListDto();

            //实例一个列表类型
            var listSource = new List<Temp_User>();

            //操作SQLSugar仓储去查询数据

            //1、按照分页取数据
            if (userListDto.pageIndex != 0 && userListDto.pageSize != 0)
            {
                var result = userRepository.GetList().Skip(userListDto.pageIndex - 1)
                    .Take(userListDto.pageSize).ToList();
                foreach (var item in result)
                {
                    listSource.Add(new Temp_User
                    {
                        user_id = item.user_id,
                        user_name = item.user_name,
                        user_pwd = item.user_pwd,
                        stop_mk = item.stop_mk,
                        datains_time = item.datains_time
                    });
                }
            }
            //2、取全部数据
            else
            {
                var result = userRepository.GetList().ToList();
                foreach (var item in result)
                {
                    listSource.Add(new Temp_User
                    {
                        user_id = item.user_id,
                        user_name = item.user_name,
                        user_pwd = item.user_pwd,
                        stop_mk = item.stop_mk,
                        datains_time = item.datains_time
                    });
                }
            }

            oUserListDto.listSource = listSource;

            return oUserListDto;
        }
    }
}
